package com.ruoyi.wisdom.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.wisdom.constants.IotConstants;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wisdom.domain.IotConfig;
import com.ruoyi.wisdom.service.IIotConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 参数配置Controller
 * 
 * @author ruoyi
 * @date 2025-01-20
 */
@RestController
@RequestMapping("/wisdom/config")
public class IotConfigController extends BaseController
{
    @Autowired
    private IIotConfigService iotConfigService;

    @Autowired
    @Qualifier("redisTemplate")
    public RedisTemplate redisTemplate;

    /**
     * 查询参数配置列表
     */
    @PreAuthorize("@ss.hasPermi('wisdom:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(IotConfig iotConfig)
    {
        startPage();
        List<IotConfig> list = iotConfigService.selectIotConfigList(iotConfig);
        return getDataTable(list);
    }

    /**
     * 导出参数配置列表
     */
    @PreAuthorize("@ss.hasPermi('wisdom:config:export')")
    @Log(title = "参数配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotConfig iotConfig)
    {
        List<IotConfig> list = iotConfigService.selectIotConfigList(iotConfig);
        ExcelUtil<IotConfig> util = new ExcelUtil<IotConfig>(IotConfig.class);
        util.exportExcel(response, list, "参数配置数据");
    }

    /**
     * 获取参数配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('wisdom:config:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(iotConfigService.selectIotConfigById(id));
    }

    /**
     * 新增参数配置
     */
    @PreAuthorize("@ss.hasPermi('wisdom:config:add')")
    @Log(title = "参数配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IotConfig iotConfig)
    {
        return toAjax(iotConfigService.insertIotConfig(iotConfig));
    }

    /**
     * 修改参数配置
     */
    @PreAuthorize("@ss.hasPermi('wisdom:config:edit')")
    @Log(title = "参数配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotConfig iotConfig)
    {
        try {
            int result = iotConfigService.updateIotConfig(iotConfig);
            // 写入缓存
            // 创建 ObjectMapper 对象
            ObjectMapper objectMapper = new ObjectMapper();
            // 步骤 2：将实体对象转换为 JSON 字符串
            String jsonString = objectMapper.writeValueAsString(iotConfig);
            // 写入缓存
            redisTemplate.opsForValue().set(IotConstants.SYSTEM_CONFIG, jsonString);

            return toAjax(result);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return toAjax(0);
        }
    }

    /**
     * 删除参数配置
     */
    @PreAuthorize("@ss.hasPermi('wisdom:config:remove')")
    @Log(title = "参数配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(iotConfigService.deleteIotConfigByIds(ids));
    }
}
